package com.guinea.account.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.account.entity.User;
import com.guinea.account.form.UserForm;
import com.guinea.account.service.RoleManager;
import com.guinea.account.service.UserManager;
import com.guinea.core.orm.BaseEntity;
import com.guinea.core.service.CRUDManager;
import com.guinea.core.web.StrutsEntityAction;
import com.guinea.email.MimeMailService;

public class UserAction extends StrutsEntityAction{
	private MimeMailService mimeMailService;

	private RoleManager roleManager;
	
	private UserManager userManager;

	public CRUDManager getEntityManager() {
		return (CRUDManager) getUserManager();
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public MimeMailService getMimeMailService() {
		return mimeMailService;
	}

	public void setMimeMailService(MimeMailService mimeMailService) {
		this.mimeMailService = mimeMailService;
	}

	public RoleManager getRoleManager() {
		return roleManager;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward saveAndMore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//验证TOKEN，防止重复提交
		if(!validateToken(request)) return list(mapping, form, request, response);
		
		Object object = initEntity(request);
		if(object == null) object = doNewEntity();
		
		//save操作前的验证
		ActionForward errorForward = validateBeforeSave(object,mapping, form, request, response);
		if(errorForward != null) return errorForward;
		
		// 将lazyform内容绑定到object
		bindEntity(form, object);
		// 为子类扩展操作
		onInitEntity(form, request, object);
		
		// 为保存操作添加创建人更新人等信息
		BaseEntity baseEntity = addSaveData(object);
		doSaveEntity(request, baseEntity);
		//添加userId
		request.setAttribute("userId",baseEntity.getId());
		saveMessage(request, "entity.saved");

		return mapping.findForward("contactInput");
	}
	
	/**
	 * 删除单个对象的Action函数.
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Object object = initEntity(request);
		if(object == null){
			saveDirectlyError(request, "操作失败：记录不存在");
			return list(mapping, form, request, response);
		}
		try {
			doDeleteEntity(request,object);
			saveMessage(request, "entity.deleted");
		} catch (Exception e) {
			saveDirectlyError(request, e.getMessage());
		}
		return mapping.findForward(SUCCESS);
	}
	
	/**从SVW导入用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward importUsersFromSvw(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
//		getUserManager().initUsers();
		return null;
	}
	/**
	 * 显示Form表单时的回调函数.为Form对象添加更多属性,在子类重载.
	 */
	public void onInitForm(ActionForm form, HttpServletRequest request,
			Object object) {
		if (id != null) {
			String[] relativeRoleIds = getUserManager()
					.getRelativeRoleIds(id);
			UserForm userForm = (UserForm) form;
			userForm.setRelativeRoleIds(relativeRoleIds);
		}
	}

	/**
	 * 保存Form表单时的回调函数.为业务对象添加更多属性，在子类重载.
	 */
	public void onInitEntity(ActionForm form, HttpServletRequest request,
			Object object) {
		User user = (User) object;
		String[] relativeRoleIds = request
				.getParameterValues("relativeRoleIds");
		user.getRoles().clear();
		for (int i = 0; relativeRoleIds != null && i < relativeRoleIds.length; i++) {
			Long roleId = new Long(relativeRoleIds[i]);
			user.getRoles().add(roleManager.get(roleId));
		}

	}

	public void doSaveEntity(HttpServletRequest request,Object object) {
		getUserManager().saveUser((User)object);
	}

	public void doEraseEntity(HttpServletRequest request,Object object) {
		getUserManager().eraseUser((User)object);
	}

	public void doDeleteEntity(HttpServletRequest request,Object object) {
		getUserManager().deleteUser((User)object);
	}
}
